const YoloDetector = require('../yolo');
const APP = require('../APP');
class index {
    androidBot = null;
    constructor(client) {
        this.GameClient = client;
        this.androidBot = this.GameClient.androidBot;
    }
    async run() {
        let done = false;
        let loding = false;
        // 耗时
        let loading_time = 0;
        while (!done) {
            let data = await YoloDetector.screen(this.androidBot);
            if(loading_time > 120){
                console.log('卡死-执行重启流程')
                continue;
            }
            let page_gameloading = APP.findClass_index(data, 'loding');
            if (page_gameloading) {
                console.log(`游戏加载中:已耗时${loading_time}s`);
                loding = true;
                loading_time += 1;
                await APP.sleep(1000);
                continue;
            }
            if (loding) {
                console.log('成功 进入游戏');
                return true;
            }
            let startGame = APP.findClass_v2(data, 'btn_text', '开始游戏');
            if (startGame) {
                await startGame.click();
            }
            let yes = APP.findClass_v2(data, 'btn_text', '接受');
            if (yes) {
                await yes.click();
                await APP.sleep(2000);
                continue;
            }
          
            await APP.sleep();
        }
        return true;
    }
}

module.exports = index;